const superMapForCesiumVariableObj = {
  viewer: null, // new Cesium.Viewer 实例
  imageryLayers: {}, // 加载的图层管理
  layerObj: {}, //
  handler: null, // 鼠标操作
};

// 中国视角
export function cameraSetViewToChina(
  viewer = superMapForCesiumVariableObj.viewer
) {
  viewer.scene.camera.setView({
    destination: new Cesium.Cartesian3(
      -3789784.091284549,
      10776489.627321979,
      8506726.890827557
    ),
    orientation: {
      heading: 6.27174666154996,
      pitch: -1.552611240189517,
      roll: 0,
    },
  });
}

// 中国视角-平滑过渡
export function cameraSetViewFlyToChina(
  viewer = superMapForCesiumVariableObj.viewer
) {
  viewer.camera.flyTo({
    destination: new Cesium.Cartesian3(
      -3789784.091284549,
      10776489.627321979,
      8506726.890827557
    ),
    orientation: {
      heading: 6.276905934686307,
      pitch: -1.5461813786090643,
      roll: 0,
    },
    duration: 3, // 动画持续时间（秒）
    easingFunction: Cesium.EasingFunction.CUBIC_IN_OUT, // 缓动函数
    complete: () => console.log("相机移动完成"), // 完成回调
    cancel: () => console.log("相机移动取消"), // 取消回调
  });
}

const pointImage = new URL("@/assets/points/椭圆x3.png", import.meta.url).href;

// 使用Entity API
let _entity = null;
let offset = new Cesium.HeadingPitchRange(
  Cesium.Math.toRadians(0), // 方位角 （0 表示正北，单位弧度）
  Cesium.Math.toRadians(-45), // 方位角 （-90 表示正式目标，单位弧度）
  30000 // 距离 （相机与目标的直线距离，单位：米）
);
export function entitieAdd(
  viewer,
  longitude,
  lattitude,
  zoomTo = true,
  metadata,
  image = pointImage
) {
  if (_entity) {
    viewer.entities.remove(_entity);
  }
  // 添加图片点位（Entity方式）
  const entity = (_entity = viewer.entities.add({
    id: "测试ID",
    point: {
      // depthTestAgainstTerrain: true,
    },
    position: Cesium.Cartesian3.fromDegrees(longitude, lattitude), // 需要是 4326 坐标系
    billboard: {
      // depthTestAgainstTerrain: true,
      disableDepthTestDistance: Number.POSITIVE_INFINITY,
      image: image,
      scale: 1,
      width: 16,
      height: 16,
      horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
      verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
      heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
      // color: Cesium.Color.YELLOW.withAlpha(0.8), // 带透明度的黄色
      pixelOffset: new Cesium.Cartesian2(0, 0), // 向上偏移10像素
      distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 5.0e7),
    },
    description: "这是一个自定义图片点位", // 鼠标悬停时的描述
    name: "测试点位", // 点位名称
    // 添加自定义属性，用于事件判断
    metadata: metadata || {
      type: "POI",
      description: "测试...",
    },
    // distanceDisplayCondition: new Cesium.DistanceDisplayCondition(1.0e3, 1.0e6),
  }));

  // 自动定位到该点位
  if (zoomTo) {
    viewer.zoomTo(entity, offset);
  }

  return entity;
}

export function entitieRemove(viewer, entity = _entity) {
  viewer.entities.remove(entity);
}

/**
 *
 * @param {*} viewer
 * @param {*} arrayHeights
 * @param {*} zoomTo
 * @returns
 */
let _lineEntity = null;
export function entitieAddLine(
  viewer = superMapForCesiumVariableObj.viewer,
  arrayHeights,
  zoomTo = true,
  metadata
) {
  if (_lineEntity) {
    viewer.entities.remove(_lineEntity);
  }

  const lineEntity = (_lineEntity = viewer.entities.add({
    name: "基础线",
    id: "基础线",
    polyline: {
      positions: Cesium.Cartesian3.fromDegreesArrayHeights([...arrayHeights]),
      //  线样式
      width: 8,
      material: Cesium.Color.RED,
      clampToGround: true,
      arcType: Cesium.ArcType.GEODESIC,
    },
    metadata: metadata || {
      type: "LINE",
      description: "测试...",
    },
    description: "description", // 鼠标悬停时的描述
  }));

  if (zoomTo) {
    viewer.zoomTo(lineEntity);
  }

  return lineEntity;
}

export function entitieLineRemove(viewer, entity = _lineEntity) {
  viewer.entities.remove(entity);
}

export default superMapForCesiumVariableObj;
